package com.crm.crmback.web.controller;

import com.crm.crmback.model.CURDResult;
import com.crm.crmback.model.CourseOrder;
import com.crm.crmback.model.PageResult;
import com.crm.crmback.service.ICourseOrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.List;

@Controller
@RequestMapping("/courseOrder")
public class CourseController {

    //注入service层
    @Autowired
    ICourseOrderService courseOrderService;
    /**
     * 跳转至课程订单列表页面
     * @return
     */
    @RequestMapping("/list")
    public String list(){
        return "courseorder/list";
    }

    /**
     *
     * @param page 表示显示的当前页
     * @param limit 每页显示多少条记录，相当于是 pageSize
     * @return
     */
    @RequestMapping("/listJson")
    @ResponseBody
//    public PageResult<CourseOrder> listJson(CourseOrder condition, int page,int limit){
//        PageResult<CourseOrder> result = new PageResult<CourseOrder>();
//        result.setCode(0);
//        result.setCount(300);
//
//        List<CourseOrder> list = new ArrayList<CourseOrder>();
//
//        CourseOrder order1 = new CourseOrder();
//        order1.setCourse_name("springBoot");
//        order1.setCourse_price("399");
//        order1.setName("A stu");
//
//        CourseOrder order2 = new CourseOrder();
//        order2.setCourse_name("SpringMVC");
//        order2.setCourse_price("236");
//        order2.setName("B stu");
//
//        list.add(order1);
//        list.add(order2);
//
//        result.setData(list);
//        PageResult<CourseOrder> result = courseOrderService.findPageResult(null, 1, 10);
    public PageResult<CourseOrder> listJson(CourseOrder condition, int page,int limit){
//        PageResult<CourseOrder> result = courseOrderService.findPageResult(null, page, limit);
        PageResult<CourseOrder> result = courseOrderService.findPageResult(condition, page, limit);
        return result;
    }

    /**
     * 添加的页面
     */
    @RequestMapping("/add")
    public String add(){
        return "courseorder/add";
    }

    /**
     *
     * @param order 页面传递过来的添加页面的数据
     * @return
     */
    @RequestMapping("/save")
    @ResponseBody
    public CURDResult save(CourseOrder order){
        CURDResult result = new CURDResult();
        if(order.getOrder_id() == null || order.getOrder_id().length() == 0){
            System.out.println("order = " + order);
            courseOrderService.save(order);
        }else{
            courseOrderService.update(order);
        }
        return result;
    }

    /**
     *
     * @param model 模型驱动
     * @param order_id 订单id
     * @return
     */
    @RequestMapping("/detail")
    public String detail(Model model,String order_id){
        System.out.println("CourseController.detail.....");
        System.out.println("order_id = " + order_id);
        CourseOrder order = courseOrderService.findByOrderId(order_id);
        model.addAttribute("order",order);
        return "courseorder/detail";
    }

    /**
     *
     * @param order_id 要删除的订单的id
     * @return
     */
    @RequestMapping("/delete")
    @ResponseBody
    public CURDResult delete(String order_id){
        CURDResult result = new CURDResult();
        courseOrderService.delete(order_id);
        return result;
    }

    /**
     * 编辑课程订单
     * @param model
     * @param order_id
     * @return
     */
    @RequestMapping("/edit")
    public String edit(Model model, String order_id) {
        CourseOrder order = courseOrderService.findByOrderId(order_id);
        model.addAttribute("order", order);

        return "courseorder/edit";

    }
}
